package com.imessage.action;


import com.imessage.jackson.JsonArray;
import com.imessage.jackson.JsonObject;
import com.imessage.type.EnumMessage;
import com.imessage.utils.EnumUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;


@Slf4j
@RestController
@RequestMapping("/select")
public class SelectAction  {


	@RequestMapping(value = "")
	public Object option(
			@RequestParam(name="className",required = true)String className,
			@RequestParam(name="exclude",required = false)String exclude) {
		Class c= null;
		try {
			c = Class.forName(className);
		} catch (Exception e) {
			log.error("",e);
		}
		Map<String, EnumMessage> enumMap=EnumUtil.getEnumValues(c);
		List<String> codeList = new ArrayList<String>(enumMap.keySet());
		Collections.sort(codeList,new Comparator(){
			@Override
			public int compare(Object o1, Object o2) {
				if(StringUtils.isNumeric(o1.toString())
						&&StringUtils.isNumeric(o2.toString())){
					if(Integer.parseInt(o1.toString())>=Integer.parseInt(o2.toString())){
						return 1;
					}else{
						return -1;
					}
				}else{
					return o1.toString().compareTo(o2.toString());
				}
			}}
		);
		JsonArray jsonArray=new JsonArray();
		for(String code:codeList){
			JsonObject jsonObject=new JsonObject();
			boolean isDisplay=true;
			if(StringUtils.isNotBlank(exclude)){
				String[] excludes=exclude.split(",");
				for(String e:excludes){
					if(code.equalsIgnoreCase(e)){
						isDisplay=false;
						break;
					}
				}
			}
			if(isDisplay){
				jsonObject.put("code", code);
				jsonObject.put("display", enumMap.get(code).getDisplay()+"("+code+")");
				jsonArray.add(jsonObject);
			}
		}
		return jsonArray;
	}



	


	
}
